<template>
	<view>
		<view class="content">
			<view class="list">
				<view class="row tx" @tap="selectImg1()">
					<view class="title">用户头像</view>
					<view class="right">
						<view class="tis">
							<image :src="idCard1"></image>
						</view>
						<view class="icon xiangyou"></view>
					</view>
				</view>
				<view class="row" @tap="toedit()">
					<view class="title">用户昵称</view>
					<view class="right">
						<view class="tis">{{user.user_name }}</view>
						<view class="icon xiangyou"></view>
					</view>
				</view>
			</view>
			<view class="list">
				<view class="row">
					<view class="button" @tap="gologin()">退出登录</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _self;
	export default {
		data() {
			return {
				idCard1: '/static/img/face.jpg',
				user: {
					user_name: '',
					truename: '',
					mobile: '',
					work: '',
					area: ''
				}
			};
		},
		onLoad() {

		},
		onShow() {
			_self = this;
			this.$api.http.get('user-info',{}).then(res=>{
				if(res){
					if (res.code == 1) {
						var userInfo = res.data.member;
						if(userInfo.head_img){
							this.idCard1=userInfo.head_img
						}
						if (userInfo.user_name) {
							this.user.user_name = userInfo.user_name;
						} else {
							this.user.user_name = "请设置用户名"
						}
						if (userInfo.truename) {
							this.user.truename = userInfo.truename;
						} else {
							this.user.truename = "未设置"
						}
						if (userInfo.mobile) {
							this.user.mobile = userInfo.mobile;
						} else {
							this.user.mobile = "未设置"
						}
					}
				}
			})
		},
		methods: {
			showType(tbIndex) {
				this.tabbarIndex = tbIndex;
				this.list = this.orderList[tbIndex];
			},
			toPage(url) {
				console.log(url)
				switch (url) {
					case "login":
						url = "../login/login";
						break;
					case "info":
						url = "/pages/user/info";
						break;
					case "logout":
						this.$store.commit('logout');
						return false;
						url = "../login/logout";
						break;
					default:
						break;
				}
				uni.navigateTo({
					url: url
				})
			},
			toedit() {
				uni.navigateTo({ 
					url: '/pages/user/setting/edit'
				})
			},
			tomobile() {
				uni.navigateTo({ 
					url: '/pages/user/setting/mobile'
				})
			},
			selectImg1: function() {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'], // 指定图片来源类型
					success: function(res) {
						_self.idCard1 = res.tempFilePaths[0];
						var uploadTask1 = uni.uploadFile({
							url: _self.websiteUrl() + 'common-upload',
							filePath: _self.idCard1,
							fileType: 'image',
							name: 'file',
							header:{"token":uni.getStorageSync("token")},
							success: function(res) {
								var dataInfo = JSON.parse(res.data);
								if(dataInfo.code !=1){
									uni.showToast({
										title: "保存失败"
									});
									return;
								} 
								uni.request({
									url: _self.websiteUrl() + 'user-edit',
									data: {
										head_img: dataInfo.data.uri
									},
									header: {
										token: uni.getStorageSync("token"),
										'content-type': 'application/json' //自定义请求头信息
									},
									method: 'POST',
									success: (res) => {
										console.log(res)
										uni.showToast({
											title: "保存成功"
										});
									}
								});
							}
						});
					}
				})
			},
			gologin() {
				uni.removeStorageSync('token');
				uni.showToast({
					title: "退出成功"
				});
				uni.switchTab({
					url: '/pages/tabBar/home/home'
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f3f3f3;
	}

	.icon {
		font-size: 30upx;

	}

	.content {
		padding-bottom: 20upx;

		.list {
			width: 92%;
			padding: 0 4%;
			background-color: #fff;
			margin-bottom: 20upx;

			.row.tx {
				min-height: 150rpx;

				.tis {
					image {
						width: 100rpx;
						height: 100rpx;
						border-radius: 100%;
						margin: 10rpx 0;
						display: block;
					}
				}
			}

			.row {
				widows: 100%;
				min-height: 110rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				border-bottom: solid 1upx #eee;

				&:last-child {
					border-bottom: none;
				}

				.title {
					font-size: 28upx;
					color: #222;
				}

				.right {
					display: flex;
					align-items: center;
					color: #666;
					line-height: 90rpx;
					height: 90rpx;

					.tis {
						font-size: 26upx;
						margin-right: 15upx;
					}

					.icon {
						color: #c8cacc;
						font-weight: bold;
						font-size: 26rpx;
					}
				}

				.button {
					background: #fff;
					border: 2upx solid #fff;
					color: #3cbdc5;
					margin: 0 auto;
					font-size: 30rpx;
					font-weight: bold;
				}

			}
		}
	}
</style>
